在 Python 中更改进程优先级,跨平台

您所在的位置:网站首页 python中and or not的优先级 在 Python 中更改进程优先级,跨平台

在 Python 中更改进程优先级,跨平台

2023-03-22 01:47| 来源: 网络整理| 查看: 265

回答问题

我有一个执行耗时计算的 Python 程序。由于它使用高 CPU,并且我希望我的系统保持响应,我希望程序将其优先级更改为低于正常值。

我发现了这个:Set Process Priority In Windows - ActiveState

但我正在寻找一个跨平台的解决方案。

Answers

这是我用来将我的进程设置为低于正常优先级的解决方案:

lowpriority.py

def lowpriority(): """ Set the priority of the process to below-normal.""" import os if os.name == 'nt': # Calls: # https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-setpriorityclass import win32process handle = win32process.GetCurrentProcess() win32process.SetPriorityClass(handle, win32process.BELOW_NORMAL_PRIORITY_CLASS) else: os.nice(1)

在 Windows 和 Linux 上的 Python 2.6/3.10 上测试。



【本文地址】


今日新闻


推荐新闻


    CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3